

export class ColorUtils {
    /** 16进制颜色转换 16进制透明颜色
    * @example (#FF6600, 0.75) => #FF6600BF
    */
    static hexColorToAlpha = (hexColor, alpha = 1) => {
        if (alpha > 1 || alpha < 0) {
            return hexColor;
        }
        const hexAlpha = ('00' + (Math.floor(alpha * 255)).toString(16)).slice(-2);
        const result = `${hexColor}${hexAlpha}`;
        return result.toLocaleUpperCase();
    }
    /** 
     * 16进制到RGB的转换 
     * @param {String} hex
     * */
    static hexToRgb = (hex) => {
        // 16进制颜色值转为RGB
        var hex = hex.replace('#', '');
        var r = parseInt(hex.substring(0, 2), 16);
        var g = parseInt(hex.substring(2, 4), 16);
        var b = parseInt(hex.substring(4, 6), 16);
        return { r, g, b };
    }
    /** 
     * RGB到HSL的转换 
     * @param {Number} r
     * @param {Number} g
     * @param {Number} b
     * */
    static rgbToHsl = (r, g, b) => {
        r /= 255, g /= 255, b /= 255;
        var max = Math.max(r, g, b), min = Math.min(r, g, b);
        let h = 0, s, l = (max + min) / 2;

        if (max === min) {
            h = s = 0; // achromatic
        } else {
            var d = max - min;
            s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
            switch (max) {
                case r: h = (g - b) / d + (g < b ? 6 : 0); break;
                case g: h = (b - r) / d + 2; break;
                case b: h = (r - g) / d + 4; break;
            }
            h /= 6;
        }

        // 将h, s, l的值四舍五入到0-360, 0-100, 0-100的范围
        h = Math.round(h * 360);
        s = Math.round(s * 100);
        l = Math.round(l * 100);

        return { h, s, l };
    }

    /** 
     * 16进制颜色转HSL颜色 
     * */
    static hexToHSL = (hex) => {
        const { r, g, b } = ColorUtils.hexToRgb(hex);
        const hsl = ColorUtils.rgbToHsl(r, g, b);
        return hsl;
    }

    /** 
     * HSL到RGB的转换 
     * @param {{ h: number, s: number, l: number }} hsl
     * */
    static hslToRgb = (hsl) => {
        let { h, s, l } = hsl;
        h /= 360;
        s /= 100;
        l /= 100;
        let r, g, b;
        if (s === 0) {
            r = g = b = l; // achromatic  
        } else {
            const hue2rgb = function hue2rgb(p, q, t) {
                if (t < 0) t += 1;
                if (t > 1) t -= 1;
                if (t < 1 / 6) return p + (q - p) * 6 * t;
                if (t < 1 / 2) return q;
                if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
                return p;
            };
            const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
            const p = 2 * l - q;
            r = hue2rgb(p, q, h + 1 / 3);
            g = hue2rgb(p, q, h);
            b = hue2rgb(p, q, h - 1 / 3);
        }
        return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
    }

    /** RGB到16进制的转换 */
    static rgbToHex(r, g, b) {
        const toHex = (x) => {
            const hex = x.toString(16);
            return hex.length === 1 ? '0' + hex : hex;
        };
        return '#' + toHex(r) + toHex(g) + toHex(b);
    }

    /** 
     * HSL颜色转16进制
     * @param {{ h: number, s: number, l: number }} hsl
     *  */
    static hslToHex = (hsl) => {
        const [r, g, b] = ColorUtils.hslToRgb(hsl);
        const hex = ColorUtils.rgbToHex(r, g, b);
        return hex;
    }
    /** 
     * 透明16进制颜色 与 背景色（不透明）融合后的颜色 
     * @param {String} hex
     * @param {String} backgroundColor
     * */
    static transparentToNonTransparent = (hex, backgroundColor) => {
        // 将16进制颜色转换为RGB
        function hexToRgb(hex) {
            let r = parseInt(hex.slice(1, 3), 16);
            let g = parseInt(hex.slice(3, 5), 16);
            let b = parseInt(hex.slice(5, 7), 16);
            let a = parseInt(hex.slice(7, 9), 16) / 255;
            return { r, g, b, a };
        }
        // 将RGB颜色转换为16进制
        function rgbToHex(r, g, b) {
            return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1).toUpperCase();
        }
        // 将背景颜色转换为RGB
        function backgroundToRgb(backgroundColor) {
            let r = parseInt(backgroundColor.slice(1, 3), 16);
            let g = parseInt(backgroundColor.slice(3, 5), 16);
            let b = parseInt(backgroundColor.slice(5, 7), 16);
            return { r, g, b };
        }
        // 计算新的RGB值
        function calculateNewColor(transparentColor, backgroundColor) {
            let { r: r1, g: g1, b: b1, a: a1 } = hexToRgb(transparentColor);
            let { r: r2, g: g2, b: b2 } = backgroundToRgb(backgroundColor);

            let r = Math.round(a1 * r1 + (1 - a1) * r2);
            let g = Math.round(a1 * g1 + (1 - a1) * g2);
            let b = Math.round(a1 * b1 + (1 - a1) * b2);

            return rgbToHex(r, g, b);
        }

        // 确保输入是有效的
        if (!/^#[0-9A-F]{8}$/i.test(hex) || !/^#[0-9A-F]{6}$/i.test(backgroundColor)) {
            throw new Error('Invalid color code');
        }

        return calculateNewColor(hex, backgroundColor);
    }
}